 
package GUI;

import java.awt.*;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Vector;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JPanel;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.view.JasperViewer;

import Messages.MessageShowAllEmployees;
import Messages.MessageShowAllEmployeesReply;
import Messages.MessageShowAllWorkHours;
import Messages.MessageShowAllWorkHoursReply;
import Reports.DataForPrintWorkingHours;
import Reports.FullBon;
import Reports.MenuesForPrintForFullBon;

/**
 * Class for creation right side panel for control of employees
 * @author jeka
 *
 */
public class PanelFunctionsClockInAdmin  extends PanelFunctionsEmpty
{
    private static final long serialVersionUID = 1L;
	private JButton button_Enter_New_Work_day;
	private JButton button_Update_Exist_Work_Day;
	private JButton button_print_work_hours;
	private JButton button_back;
	private MainMenu mainMenu;
	private PfDoh_work_hoursAdmin workHours;
	private int flagButtonPress=1;
	
public PanelFunctionsClockInAdmin(MainMenu mm,PfDoh_work_hoursAdmin empHours)
{
	super();	
	this.mainMenu=mm;
	this.workHours=empHours;
	this.add(get_button_insert_day());
	this.add(get_button_edit_day());
	this.add(get_button_print());
	this.add(get_button_back());
	 
}
	
	
	
/**
 * Method for creation JButton	button_Enter_New_Work_day that open new right side panel in this
 * window for entering new work day 
 * @return button_Enter_New_Work_day
 */
private JButton get_button_insert_day() 
	{
		if (button_Enter_New_Work_day == null) 
		{			
			button_Enter_New_Work_day=new JButton("הכנסת יום עבודה");  			 
			button_Enter_New_Work_day.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 15));
			button_Enter_New_Work_day.setPreferredSize(new Dimension(220, 50));
			button_Enter_New_Work_day.addActionListener(new java.awt.event.ActionListener() 
			   {
				public void actionPerformed(java.awt.event.ActionEvent e) 
				{
					workHours.addInsertWorkDayPanel();			
				}
			});
		}
		return button_Enter_New_Work_day;
	}
	
	
	
/**
 * Method for creation JButton 	button_Update_Exist_Work_Day that open new right side
 * panel in this window for updating selected work day
 * @return button_Update_Exist_Work_Day
 */
private JButton get_button_edit_day() 
	{
		if (button_Update_Exist_Work_Day == null) 
		{	 
			button_Update_Exist_Work_Day=new JButton("עדכון שעות עבודה");  			 
			button_Update_Exist_Work_Day.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 15));
			button_Update_Exist_Work_Day.setPreferredSize(new Dimension(220, 50));
			
			button_Update_Exist_Work_Day.addActionListener(new java.awt.event.ActionListener() 
			   {
				public void actionPerformed(java.awt.event.ActionEvent e) 
				{
					workHours.addChangeWorkDayPanel();	
				}
			});
		}
		return button_Update_Exist_Work_Day;
	}
	
private JButton get_button_print()    
{
	if (button_print_work_hours == null) 
	{	
		Icon heshbonIcon = new ImageIcon("print-icon.png");
		Image img = ((ImageIcon) heshbonIcon).getImage();
		Image newimg = img.getScaledInstance(40, 40,java.awt.Image.SCALE_SMOOTH);
		heshbonIcon = new ImageIcon(newimg);
	  
		button_print_work_hours=new JButton("הדפסת ימי עבודה",heshbonIcon);				 
		button_print_work_hours.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 15));
		button_print_work_hours.setPreferredSize(new Dimension(220, 50));
		button_print_work_hours.addActionListener(new java.awt.event.ActionListener() 
		   {
			public void actionPerformed(java.awt.event.ActionEvent e) 
			{
				Date date = new Date();
				 DateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
				 String   data = df.format(date);
								
				 DateFormat dfmonth = new SimpleDateFormat("MM");
				 String   month = dfmonth.format(date);
				
				 
				 DataForPrintWorkingHours dataWrk = new DataForPrintWorkingHours();
				 MessageShowAllWorkHours msgShowHours=new MessageShowAllWorkHours((String)workHours.allHours.get(0).get(0), month); //EID of current employee & month 
				 try {
					mainMenu.ms.client.sendToServer(msgShowHours);
					MessageShowAllWorkHoursReply result2 = (MessageShowAllWorkHoursReply) mainMenu.ms.client.GetMessage();
					dataWrk.setAllHoursOfThisMonth(result2.getEmployeeHours());  // contains emp.name,ewh.date,ewh.clock_in,ewh.clock_out,ewh.comments
					 
					 System.out.println(dataWrk.getAllHoursOfThisMonth().get(0));
				 
				 } catch (IOException e2) {
					// TODO Auto-generated catch block
					e2.printStackTrace();
				}
				 
				
				 ArrayList<DataForPrintWorkingHours> dataList = new ArrayList<DataForPrintWorkingHours>();
				 dataWrk.setDate(data);
				 
				 //select customer_name as NAME, id_customer as ID from customer where customer_role in ($P{roles})
				 //select * from customer where $X{IN,customer_role,roles}
				 // create $var in jasper 
				 
				 //Map parametersMap = new HashMap();    // stackoverflow
				 //parametersMap.put("id",7);
				 
				 //JasperPrint jasperPrint = JasperFillManager.fillReport(
				 //jasperReport, parametersMap, jdbcConnection);
				 
				 dataList.add(dataWrk);
				 JRBeanCollectionDataSource reportData = new JRBeanCollectionDataSource(
							dataList);
					JasperPrint print;
					try {
						print = JasperFillManager
								.fillReport(
										"Reports/Orders/printWorkingHours.jasper",
										null, reportData);
						JasperViewer.viewReport(print);
						
					} catch (JRException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}	
					
					
				
			}
		});
	} 
	return button_print_work_hours;
}

private JButton get_button_back() 
{
	if (button_back == null) 
	{			
		button_back=new JButton("חזרה");  			 
		button_back.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 15));
		button_back.setPreferredSize(new Dimension(220, 50));
		button_back.addActionListener(new java.awt.event.ActionListener() 
		   {
			public void actionPerformed(java.awt.event.ActionEvent e) 
			{
				 
				
				((BorderLayout) mainMenu
						.getContentPane().getLayout())
						.getLayoutComponent(BorderLayout.CENTER).setVisible(false);

				MessageShowAllEmployees msgShowEmp = new MessageShowAllEmployees();

				try {
					mainMenu.ms.client.sendToServer(msgShowEmp);
					MessageShowAllEmployeesReply result = (MessageShowAllEmployeesReply) mainMenu.ms.client
							.GetMessage();
					if(((BorderLayout) mainMenu.getContentPane().getLayout()).getLayoutComponent(BorderLayout.EAST)!=null)
					{
					mainMenu.getContentPane()
							.remove(((BorderLayout) mainMenu
									.getContentPane().getLayout())
									.getLayoutComponent(BorderLayout.EAST));
					}
					mainMenu.getContentPane()
							.remove(((BorderLayout) mainMenu
									.getContentPane().getLayout())
									.getLayoutComponent(BorderLayout.CENTER));
					mainMenu.getContentPane().add(
							new PfSearch_employee(mainMenu, result
									.getResult()),
							BorderLayout.CENTER);
					mainMenu.getContentPane().add(
							new PanelFunctionsEmployees(mainMenu),
							BorderLayout.EAST);
					mainMenu.getContentPane().validate();

				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				
				
			}
		});
	}
	return button_back;
}


}
